﻿using System.IO;
using Baggr.FeedParser.Rss;

namespace Baggr.FeedParser
{
    using System;
    using System.Linq;
    using System.Xml;
    using NUnit.Framework;

    /// <summary>
    /// Тест стандартного файла в формате rss 2.0 из спецификации.
    /// </summary>
    public class StandartRss20Test
    {
        /// <summary>
        /// Открытый фид.
        /// </summary>
        private readonly RssFeed _Feed;

        /// <summary>
        /// Конструктор теста.
        /// </summary>
        public StandartRss20Test()
        {
            XmlReader reader = XmlReader.Create(new StringReader(Resources.rss2sample));
            _Feed = (RssFeed)reader.GetFeed();
        }

        /// <summary>
        /// Протестирвоать информацию о канале.
        /// </summary>
        [TestCase]
        public void TestFeedData()
        {
            Assert.AreEqual(_Feed.Channel.Title, "Liftoff News");
            Assert.AreEqual(_Feed.Channel.Link, "http://liftoff.msfc.nasa.gov/");
            Assert.AreEqual(_Feed.Channel.Description, "Liftoff to Space Exploration.");
            Assert.AreEqual(_Feed.Channel.LastBuildDate, "Tue, 10 Jun 2003 09:41:01 GMT");
        }

        /// <summary>
        /// Протестировать распаршены
        /// </summary>
        [TestCase]
        public void ItemsTest()
        {
            var items = new[]
                            {
                                new
                                    {
                                        title = "Star City",
                                        link = new Uri("http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp"),
                                        description = "How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's <a href=\"http://howe.iki.rssi.ru/GCTC/gctc_e.htm\">Star City</a>.",
                                        pubDate = DateTime.Parse("Tue, 03 Jun 2003 09:39:21 GMT"),
                                        guid = "http://liftoff.msfc.nasa.gov/2003/06/03.html#item573".GetGuidByString()
                                    },
                                new
                                    {
                                        title = string.Empty,
                                        link = (Uri)null,
                                        description = "Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href=\"http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm\">partial eclipse of the Sun</a> on Saturday, May 31st.",
                                        pubDate = DateTime.Parse("Fri, 30 May 2003 11:06:42 GMT"),
                                        guid = "http://liftoff.msfc.nasa.gov/2003/05/30.html#item572".GetGuidByString()
                                    },
                                new
                                    {
                                        title = "The Engine That Does More",
                                        link = new Uri("http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp"),
                                        description = "Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly.  The proposed VASIMR engine would do that.",
                                        pubDate = DateTime.Parse("Tue, 27 May 2003 08:37:32 GMT"),
                                        guid = "http://liftoff.msfc.nasa.gov/2003/05/27.html#item571".GetGuidByString()
                                    },
                                new
                                    {
                                        title = "Astronauts' Dirty Laundry",
                                        link = new Uri("http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp"),
                                        description = "Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them.  Instead, astronauts have other options.",
                                        pubDate = DateTime.Parse("Tue, 20 May 2003 08:56:02 GMT"),
                                        guid = "http://liftoff.msfc.nasa.gov/2003/05/20.html#item570".GetGuidByString()
                                    }
                            }; 
            var realItems =
                _Feed.Channel.Items.Cast<IFeedItem>().Select(
                    curr =>
                    new
                        {
                            title = curr.Title,
                            link = curr.Link,
                            description = curr.Description,
                            pubDate = curr.PublicationDate,
                            guid = curr.LocalId
                        });
            CollectionAssert.AreEquivalent(items, realItems);
        }
    }
}


